(function ($) {

    $.fn.skartOpenModal = function (options) {

        // Establish our default settings
        var mySettings = $.extend({
            title: "Modal",
            url: null,
            data: {},
            async: true,
            beforeSend: null,
            error: null,
            success: null,
            complete: null,
            modalSizeClass: "" //modal-lg, modal-sm, modal-full
        }, options);

        var myThis = $(this);
        myThis.attr("aria-expanded", "true").attr("tabindex", "-1");
        myThis.html("<div class='vertical-alignment-helper'><div class='modal-dialog " + mySettings.modalSizeClass + " vertical-align-center'><div class='modal-content'><div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-hidden='true'></button><h4 class='modal-title'>" + mySettings.title + "</h4></div><div class='modal-body-footer'></div></div></div></div>");
        myThis.find(".modal-body-footer").html("<div class='blockUI blockMsg blockElement' style='text-align:center; margin:10px auto;'><div class='loading-message loading-message-boxed'><img src='../../../../../Content/Template/assets/global/img/loading-spinner-grey.gif' align=''><span>&nbsp;&nbsp;LOADING...</span></div></div>");
        //myThis.find(".modal-body-footer").html("<div class='pull-center' style='margin:30px;'><img src='../../../../../Content/Images/ajax-loader.gif'/></div>");

        myThis.modal({
            backdrop: "static",
            keyboard: false,
            show: true
        });

        $.ajax({
            url: mySettings.url,
            data: mySettings.data,
            beforeSend: function (jqXhr, settings) {
                if (mySettings.beforeSend != null) {
                    mySettings.beforeSend.call(null, jqXhr, settings);
                }
            },
            error: function (jqXhr, textStatus, errorThrown) {
                if (mySettings.error != null) {
                    mySettings.error.call(null, jqXhr, textStatus, errorThrown);
                }
            },
            success: function (data, textStatus, jqXhr) {
                CheckAuth(data);
                
                myThis.find(".modal-body-footer").html(data);
                $.validator.unobtrusive.parse("form");

                if (mySettings.success != null) {
                    mySettings.success.call(null, data, textStatus, jqXhr);
                }
            },
            complete: function (jqXhr, textStatus) {
                if (mySettings.complete != null) {
                    mySettings.complete.call(null, jqXhr, textStatus);
                }
            }
        });
    }

}(jQuery));